#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <errno.h>
#include <time.h>
#include <pthread.h>
#include <fcntl.h>
#include <ctype.h>

#define SERV_IP "192.168.1.68"
#define SERVPORT 9999
#define BACKLOG 10

void* recv_thread(void*);
int
main(int argc,char *argv[])
{
	int sockfd,client_fd;
	struct sockaddr_in my_addr;//本机地址

	if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
	{
		perror("creat error");
		printf("Create socket error:%s\n", strerror(errno));
		return -1;
	}
	//init
	my_addr.sin_family = AF_INET;
	my_addr.sin_addr.s_addr = inet_addr(SERV_IP);

	printf("%u\n", inet_addr(SERV_IP));
	my_addr.sin_port = htons(SERVPORT);
	bzero(&(my_addr.sin_zero),8);

	if(-1 == bind(sockfd, (struct sockaddr*)&my_addr, sizeof(struct sockaddr)))
	{
		printf("bind error:%s\n",strerror(errno));
		return -1;
	}

	if(-1 == listen(sockfd, BACKLOG))
	{
		printf("listen error:%s\n",strerror(errno));
		return -1;
	}

	int listen_flag = fcntl(sockfd, F_GETFL,0);//将监听套接字设置为非阻塞模式
	listen_flag |= O_NONBLOCK;
	fcntl(sockfd, F_SETFL, listen_flag);

	while(1)
	{
		fd_set accept_set;				//监控的文件描述符集合
		FD_ZERO(&accept_set);			//初始化
		FD_SET(sockfd, &accept_set);	//将sockfd添加至监控集合

		struct timeval accept_timeout;	//设置超时时间
		accept_timeout.tv_sec = 1;		//1秒
		accept_timeout.tv_usec = 0; 	//0微秒
		int n_ready = select(sockfd + 1, &accept_set,NULL,NULL, &accept_timeout);
		if(-1 == n_ready)//返回-1说明select函数出错
		{
			break;
		}
		if(0 == n_ready) //准备好的文件描述符个数
		{
			printf("no client\n");
			continue;
		}

		struct sockaddr_in remote_addr;//客户端地址
		memset(&remote_addr, 0, sizeof(struct sockaddr_in));
		socklen_t sock_size = sizeof(struct sockaddr);

		client_fd = accept(sockfd,(struct sockaddr*)&remote_addr, &sock_size);
		if(-1 == client_fd)
		{
			printf("accept error:%s\n",strerror(errno));
			return -1;
		}
		printf("get new client:ip [%s] port [%d]\n",
				inet_ntoa(remote_addr.sin_addr),
				htons(remote_addr.sin_port));
		int flag = fcntl(client_fd, F_GETFL,0);
		flag |= O_NONBLOCK;
		fcntl(client_fd, F_SETFL, flag);
		pthread_t pid_recv;
		pthread_create(&pid_recv, NULL, recv_thread, (void*)&client_fd);
		pthread_detach(pid_recv);
	}
	return 0;
}

void* recv_thread(void* in_pv)
{
	int fd = *(int*)in_pv;
	printf("new fd =[%d]\n", fd);
	char buf[2048] = {0};
	fd_set r_set;
	struct timeval timeout;
	while(1)
	{
		FD_ZERO(&r_set);
		FD_SET(fd, &r_set);
		timeout.tv_sec = 1;
		timeout.tv_usec = 0;
		int n_ready = select(fd+1,&r_set,NULL,NULL,&timeout);
		if(-1 == n_ready)
		{
			break;
		}
		if(0 == n_ready)
		{
			printf("nothing\n");
			continue;
		}
		memset(buf,0,2048);

		int i_recv_bytes = recv(fd, &buf, 2048, 0);

		if (2048 != i_recv_bytes)
		{
			printf("real size of recv = %d\n", i_recv_bytes);
			sleep(100);
		}

		if (0 == i_recv_bytes)
		{
			printf("client disconnected:%s\n", strerror(errno));
			break;
		}

		if (-1 == i_recv_bytes)
		{
			struct sockaddr_in serv_peer;
			socklen_t t_len = sizeof(struct sockaddr);
			getpeername(fd, (struct sockaddr*)&serv_peer, &t_len);
			printf("recv error remote ip = [%s] [%s]\n",
					inet_ntoa(serv_peer.sin_addr),
					strerror(errno));
			break;
		}

	}
	close(fd);
	return NULL;
}
